package com.dream.model.stock;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Type;

import com.dream.model.app.AppCodeItem;
import com.dream.model.base.AppUserModelBase;
import com.dream.model.master.ProductPart;
import com.dream.model.master.Shop;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Entity
@Table(name="t_mst_product_part_stock")
public class ProductPartStock extends AppUserModelBase implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1975445632667183764L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private String id ;
	
	@Column(name="stock_type")
	@Enumerated(EnumType.STRING)
	private StockProductType stockType = StockProductType.new_product;

	@Column(name="pro_part_id")
	private String productPartId;
	
	@Column(name="shop_id")
	private String shopId;
	
	@Column(name="stock_id")
	private Long stockId;
	
	@Column(name="stock")
	private Integer stock = 0;
	
	@Column(name="deleted")
	@Type(type = "org.hibernate.type.NumericBooleanType")
	private Boolean deleted = false;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name="pro_part_id",updatable=false,insertable=false)
	private ProductPart productPart;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name="shop_id",updatable=false,insertable=false)
	private Shop shop;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name="stock_id",updatable=false,insertable=false)
	private AppCodeItem stockObj;
	
	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public Long getStockId() {
		return stockId;
	}

	public void setStockId(Long stockId) {
		this.stockId = stockId;
	}

	
	public AppCodeItem getStockObj() {
		return stockObj;
	}

	public void setStockObj(AppCodeItem stockObj) {
		this.stockObj = stockObj;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public StockProductType getStockType() {
		return stockType;
	}

	public void setStockType(StockProductType stockType) {
		this.stockType = stockType;
	}

	public ProductPart getProductPart() {
		return productPart;
	}

	public void setProductPart(ProductPart productPart) {
		this.productPart = productPart;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getProductPartId() {
		return productPartId;
	}

	public void setProductPartId(String productPartId) {
		this.productPartId = productPartId;
	}

	public Integer getStock() {
		return stock;
	}

	public void setStock(Integer stock) {
		this.stock = stock;
	}

	public Boolean getDeleted() {
		return deleted;
	}

	public void setDeleted(Boolean deleted) {
		this.deleted = deleted;
	}
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProductPartStock other = (ProductPartStock) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	public static enum StockProductType{new_product,old_product;
	  private static final Map<String,StockProductType> map = Maps.newConcurrentMap();
	  private static final List<String> li = Lists.newArrayList();
	  static{
		  for(StockProductType item :values()){
			  map.put(item.name(), item);
			  li.add(item.name());
		  }
	  }
	 
	  public static List<String> getTypes(){
		  return li;
	  }
	  
	  public static StockProductType getInstance(String name){
		  if(StringUtils.isBlank(name)) return null;
		  return map.get(name);
	  }
	}
}
